package com.zhongxin.wymovieticketfragment;

import com.zhongxin.wymovieticket.LoginActivity;
import com.zhongxin.wymovieticket.R;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.Toast;

public class ClientFragment extends Fragment implements OnClickListener {

	private Button loginBt, picturebt, popwindowMiss, camerabt, selectbt;
	private PopupWindow mpopupWindow;
	// 屏幕的width
	private int mScreenWidth;
	// 屏幕的height
	private int mScreenHeight;
	// PopupWindow的width
	private int mPopupWindowWidth;
	// PopupWindow的height
	private int mPopupWindowHeight;

	public ClientFragment() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater
				.inflate(R.layout.fragment_client, container, false);
		loginBt = (Button) view.findViewById(R.id.fragment_client_login);
		loginBt.setOnClickListener(this);
		picturebt = (Button) view.findViewById(R.id.fragment_client_picture);
		picturebt.setOnClickListener(this);
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.fragment_client_login:
			Intent loginIntent = new Intent(getActivity(), LoginActivity.class);
			startActivity(loginIntent);
			break;
		case R.id.fragment_client_picture:
			Toast.makeText(getActivity(), "nihao", Toast.LENGTH_LONG).show();
			getPopupWindowInstance();
			mpopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
			break;
		case R.id.popwindow_miss:
			mpopupWindow.dismiss();
			break;
		case R.id.popwindow_camera:
			Intent intentCamera = new Intent(Intent.ACTION_CAMERA_BUTTON, null);
			getActivity().sendBroadcast(intentCamera);

			break;
		case R.id.popwindow_select:
			Intent pictureIntent = new Intent();
			pictureIntent.setType("image/*");
			pictureIntent.setAction(Intent.ACTION_GET_CONTENT);
			startActivityForResult(pictureIntent, 11);

			break;
		default:
			break;
		}

	}

	/*
	 * 创建PopupWindow
	 */
	private void initPopuptWindow() {
		LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
		View popupWindow = layoutInflater.inflate(R.layout.popwindow, null);
		popwindowMiss = (Button) popupWindow.findViewById(R.id.popwindow_miss);
		popwindowMiss.setOnClickListener(this);
		camerabt = (Button) popupWindow.findViewById(R.id.popwindow_camera);
		camerabt.setOnClickListener(this);
		selectbt = (Button) popupWindow.findViewById(R.id.popwindow_select);
		selectbt.setOnClickListener(this);
		// 创建一个PopupWindow
		// 参数1：contentView 指定PopupWindow的内容
		// 参数2：width 指定PopupWindow的width
		// 参数3：height 指定PopupWindow的height
		// 获取屏幕和PopupWindow的width和height
		mScreenWidth = getActivity().getWindowManager().getDefaultDisplay()
				.getWidth();
		mScreenHeight = getActivity().getWindowManager().getDefaultDisplay()
				.getHeight();
		mpopupWindow = new PopupWindow(popupWindow, mScreenWidth, mScreenHeight);
		mPopupWindowWidth = mpopupWindow.getWidth();
		mPopupWindowHeight = mpopupWindow.getHeight();

	}

	/*
	 * 获取PopupWindow实例
	 */
	private void getPopupWindowInstance() {
		if (null != mpopupWindow) {
			mpopupWindow.dismiss();
			return;
		} else {
			initPopuptWindow();
		}
	}
}
